
class MyUtil
  def self.lock_file(f, timeout=60, interval=0.05)
    0.upto(timeout) do |i|
      if f.flock(File::LOCK_EX|File::LOCK_NB)
        return
      end
      sleep interval
    end
    raise
  end

  def self.save_obj(obj,fname,mode="w+b")
    obj = Marshal.dump(obj)
    open(fname, mode) {|f|
      lock_file(f)
      f.print obj#.to_yaml()
      f.flock(File::LOCK_UN)
    }
  end

  def self.load_obj(fname)
    open(fname) {|f|
      Marshal.load f
    }
  end
end
